﻿Imports WeifenLuo.WinFormsUI.Docking
Imports Microsoft.Office.Interop.Outlook
Imports ICSharpCode.SharpZipLib.Zip
Imports System.IO
Imports ICSharpCode.SharpZipLib.Core

Public Class PropertyForm
  Inherits DockContent
  Implements IChangeListener

  Private _service_manager As ServiceManager
  Private _wallpaper As Wallpaper
  Private _isfavmodel As Boolean = False
  Private _isfavpic As Boolean = False
  Private _config As Configuration = Configuration.GetObject

  Public Sub SetWallpaperInformation(ByRef w As Wallpaper)
    Me.PropertyGrid1.SelectedObject = w
    _wallpaper = w
    Try
      Me.TabText = "Eigenschaften zu " & w.Filename
      If w.FullName <> "" Then
        Me.SetWallpaperButton.Visible = True
        Me.Button1.Text = "Mehr von " & w.FullName
        Me.Button1.Visible = True

        _isfavmodel = _service_manager.GetWallpaperService.IsFavoriteModel(w)
        _isfavpic = _service_manager.GetWallpaperService.IsFavoritePic(w)

        ImageFavButton.Visible = True
        If _isfavpic = False Then
          Me.ImageFavButton.Text = "Bild zu Favoriten hinzufügen"
        Else
          Me.ImageFavButton.Text = "Bild aus Favoriten entfernen"
        End If

        ModelFavButton.Visible = True
        If _isfavmodel = False Then
          Me.ModelFavButton.Text = "Model zu Favoriten hinzufügen"
        Else
          Me.ModelFavButton.Text = "Model aus Favoriten entfernen"
        End If

        Me.Button2.Text = "Mehr von " & w.FullName & " aus RSS laden"
        Me.Button2.Visible = True
      End If
    Catch ex As System.Exception
      Me.TabText = "Eigenschaften"
      Me.Button1.Visible = False
      ModelFavButton.Visible = False
      ImageFavButton.Visible = False
      Me.SetWallpaperButton.Visible = False
      Me.Button2.Visible = False
    End Try
    Refresh()
  End Sub

  Private Sub PropertyForm_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
    Me.PropertyGrid1.Size = New System.Drawing.Size(Me.Width - 20, Me.PropertyGrid1.Height)
    Me.SetWallpaperButton.Size = New System.Drawing.Size(Me.Width - 20, Me.SetWallpaperButton.Height)
    Me.Button1.Size = New System.Drawing.Size(Me.Width - 20, Me.Button1.Height)
    Me.ImageFavButton.Size = New System.Drawing.Size(Me.Width - 20, Me.ImageFavButton.Height)
    Me.ModelFavButton.Size = New System.Drawing.Size(Me.Width - 20, Me.ModelFavButton.Height)
    Me.Button2.Size = New System.Drawing.Size(Me.Width - 20, Me.Button2.Height)
    Me.MailButton.Size = New System.Drawing.Size(Me.Width - 20, Me.MailButton.Height)
    Me.SendModelButton.Size = New System.Drawing.Size(Me.Width - 20, Me.SendModelButton.Height)
    Me.ZipExportButton.Size = New System.Drawing.Size(Me.Width - 20, Me.ZipExportButton.Height)
  End Sub

  Private Sub SetWallpaperButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetWallpaperButton.Click
    Cursor = Cursors.WaitCursor
    _wallpaper.SetzeWallpaper()
    'ServiceManager.GetServiceManager.GetWallpaperService.IncrementWallpaperSet(_wallpaper)
    'SetWallpaperInformation(ServiceManager.GetServiceManager.GetWallpaperService.GetWallpaperById(_wallpaper.ID))
    Cursor = Cursors.Default
  End Sub

  Private Sub PropertyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.TabText = "Eigenschaften"
    _service_manager = ServiceManager.GetServiceManager
  End Sub

  Private Sub ImageViewForm_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
    MdiMain._pf.SetWallpaperInformation(_wallpaper)
    MdiMain.Text = "Wallpaper Manager - " & Me.TabText
  End Sub

  Private Sub ImageViewForm_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
    MdiMain.Text = "Wallpaper Manager"
  End Sub

  Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    MdiMain._lwf.LoadList(_wallpaper.Name & "%", _wallpaper.FullName)
    Me.Parent.Refresh()
  End Sub

  Private Sub ImageFavButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImageFavButton.Click
    If _isfavpic = True Then
      _service_manager.GetWallpaperService.RemoveFavoritePic(_wallpaper)
      Me.ImageFavButton.Text = "Bild zu Favoriten hinzufügen"
    Else
      _service_manager.GetWallpaperService.SetFavoritePic(_wallpaper)
      Me.ImageFavButton.Text = "Bild aus Favoriten entfernen"
    End If
    Refresh()
  End Sub

  Private Sub ModelFavButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModelFavButton.Click
    If _isfavmodel = True Then
      _service_manager.GetWallpaperService.RemoveFavoriteModel(_wallpaper)
      Me.ModelFavButton.Text = "Model zu Favoriten hinzufügen"
    Else
      _service_manager.GetWallpaperService.SetFavoriteModel(_wallpaper)
      Me.ModelFavButton.Text = "Model aus Favoriten entfernen"
    End If
    Refresh()
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim wrss As New RSSByNameForm
    wrss.Text = Utils.InitCap(_wallpaper.Name) & " RSS [" & _config.Resolution & "]"
    wrss.DownloadabeWallpapers = ServiceManager.GetServiceManager.GetWallpaperService.GetListOfNew("http://www.skins.be/feeds/de/" & _wallpaper.Name & ".xml")
    wrss.Show(MdiMain.DockPanel1)
  End Sub

  Public Sub DoChange(ByVal o As Object) Implements IChangeListener.DoChange
    SetWallpaperInformation(o)
  End Sub

  Public Sub NotifyChange(ByVal c As Boolean) Implements IChangeListener.NotifyChange

  End Sub

  Private Sub MailButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MailButton.Click
    Dim m As MailItem = Outlook.GetObject.NewMail
    m.Body = "Automatisch generierte Mail zum Versenden der Datei: " & _wallpaper.Filename
    m.Attachments.Add(_config.SkinsPath & _wallpaper.Filename)
    m.Subject = "File: " & _wallpaper.Filename
    m.Display(True)
  End Sub

  Private Sub SendModelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendModelButton.Click
    Dim m As MailItem = Outlook.GetObject.NewMail
    Dim w() As Wallpaper
    m.Body = "Automatisch generierte Mail zum Versenden aller Bilder von " & _wallpaper.FullName & " mit der Auflösung " & _config.Resolution & vbNewLine
    w = _service_manager.GetWallpaperService.GetWallpaperByName(_wallpaper.Name).ToArray
    For i As Integer = 0 To w.Length - 1 Step 1
      m.Attachments.Add(_config.SkinsPath & w(i).Filename)
      m.Body &= vbNewLine & "- " & w(i).Filename
    Next
    m.Attachments.Add(_config.SkinsPath & _wallpaper.Filename)
    m.Subject = _wallpaper.FullName
    m.Display(True)
  End Sub

  Private Sub ZipExportButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZipExportButton.Click
    Dim zcf As New ZipCreateForm(_wallpaper)
    'zcf.StartCreateZIP(_wallpaper)
    Try
      zcf.Show()
    Catch ex As ObjectDisposedException
    End Try
  End Sub
  
  Private Sub FormClosing_Eventhandler(sender As Object, e As FormClosingEventArgs) handles mybase.FormClosing 
  	e.Cancel = true 
  End Sub
End Class